package com.jfeat.am.module.wegou.api.crud;

import com.baomidou.mybatisplus.plugins.Page;
import com.jfeat.am.common.constant.tips.SuccessTip;
import com.jfeat.am.common.constant.tips.Tip;
import com.jfeat.am.common.controller.BaseController;
import com.jfeat.am.core.jwt.JWTKit;
import com.jfeat.am.module.wegou.api.patch.RestaurantStatus;
import com.jfeat.am.module.wegou.services.crud.filter.ProductStoreFilter;
import com.jfeat.am.module.wegou.services.crud.service.ProductStoreService;
import com.jfeat.am.module.wegou.services.domain.model.ProductStoreModel;
import com.jfeat.am.module.wegou.services.domain.service.QueryProductStoreService;
import com.jfeat.am.module.wegou.services.patch.WeGouPatchService;
import com.jfeat.am.module.wegou.services.persistence.model.ProductStore;
import com.jfeat.am.module.wegou.services.persistence.model.RestaurantTag;
import com.jfeat.am.module.wegou.services.persistence.model.TagManage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 料理店铺 api
 * </p>
 *
 * @author Code Generator
 * @since 2018-01-03
 */
@Api("料理店铺")
@RestController
@RequestMapping("/api")
public class ProductRestaurantEndpoint extends BaseController {

    @Resource
    ProductStoreService productStoreService;
    @Resource
    WeGouPatchService weGouPatchService;
    @Resource
    QueryProductStoreService queryProductStoreService;


    @ApiOperation(value = "料理店铺 查看料理店铺详情 返回所有属于该店铺的 料理 以及 店铺的 标签", response = ProductStore.class)
    @GetMapping("/restaurant/{id}")
    public Tip getProductRestaurant(@PathVariable Long id) {
        long userId = JWTKit.getUserId(getHttpServletRequest());
        //  浏览店铺详情时 自动插入 关注数据
        weGouPatchService.insertCare(userId, id);
        return SuccessTip.create(productStoreService.showRestaurant(id));
    }

    @ApiOperation(value = "料理店铺 修改料理店铺信息", response = ProductStore.class)
    @PutMapping("/restaurant/{id}")
    public Tip updateProductRestaurant(@PathVariable Long id, @RequestBody ProductStoreModel entity) {
        entity.setId(id);
        List<TagManage> tagManages = entity.getTags();
        for (TagManage tagManage : tagManages) {
            RestaurantTag restaurantTag = new RestaurantTag();
            restaurantTag.setStoreId(id);
            restaurantTag.setTagId(tagManage.getId());
            entity.getRestaurantTags().add(restaurantTag);
        }
        return SuccessTip.create(productStoreService.updateMaster(entity, new ProductStoreFilter(), null, null));
    }

    @ApiOperation(value = "料理店铺 修改料理店铺状态", response = ProductStore.class)
    @PutMapping("/restaurant/updateStatus/{id}")
    public Tip updateProductRestaurantStatus(@PathVariable Long id) {
        ProductStore productRestaurant = productStoreService.retrieveMaster(id);
        if (productRestaurant.getStatus().equals(RestaurantStatus.RISE.toString())) {
            productRestaurant.setStatus(RestaurantStatus.DOWN.toString());
        } else {
            productRestaurant.setStatus(RestaurantStatus.RISE.toString());
        }
        return SuccessTip.create(productStoreService.updateMaster(productRestaurant));
    }

    @ApiOperation(value = "料理店铺 修改料理店铺锁定状态", response = ProductStore.class)
    @PutMapping("/restaurant/updateLocked/{id}")
    public Tip updateProductRestaurantLocked(@PathVariable Long id) {
        ProductStore productRestaurant = productStoreService.retrieveMaster(id);
        if (productRestaurant.getLocked().equals(0)) {
            productRestaurant.setLocked(1);
        } else {
            productRestaurant.setLocked(0);
        }
        return SuccessTip.create(productStoreService.updateMaster(productRestaurant));
    }

    @ApiOperation(value = "料理店铺 删除某个料理店铺", response = ProductStore.class)
    @DeleteMapping("/restaurant/{id}")
    public Tip deleteProductRestaurant(@PathVariable Long id) {
        return SuccessTip.create(productStoreService.deleteMaster(id));
    }

    @ApiOperation(value = "料理店铺 查询料理店铺 根据店铺的标签值来查询 tagName/zhTagName", response = ProductStore.class)
    @GetMapping("/restaurant")
    public Tip findRestaurants(Page page,
                               @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(name = "storeName", required = false) String storeName,
                               @RequestParam(name = "zhStoreName", required = false) String zhStoreName,
                               @RequestParam(name = "status", required = false) String status,
                               @RequestParam(name = "locked", required = false) Integer locked,
                               @RequestParam(name = "tagName", required = false) String tagName,
                               @RequestParam(name = "tagZhName", required = false) String tagZhName) {
        ProductStore productRestaurant = new ProductStore();
        productRestaurant.setStoreName(storeName);
        productRestaurant.setZhStoreName(zhStoreName);
        productRestaurant.setStatus(status);
        productRestaurant.setLocked(locked);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page.setRecords(queryProductStoreService.findRestaurantByTag(page, productRestaurant, tagName, tagZhName));
        return SuccessTip.create(page);
    }


    ///  暂时不需要用到 此方法  考虑以后可能会使用，暂时保留
    /*@GetMapping
    //此方法可能需要自行添加需要的参数,按需要使用
    public Tip queryProductRestaurants(Page<ProductRestaurant> page,
                                       @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                       @RequestParam(name = "id", required = false) Long id,
                                       @RequestParam(name = "logo", required = false) String logo,
                                       @RequestParam(name = "cover", required = false) String cover,
                                       @RequestParam(name = "pushPhoto", required = false) String pushPhoto,
                                       @RequestParam(name = "restaurantName", required = false) String restaurantName,
                                       @RequestParam(name = "zh_restaurantName", required = false) String zh_restaurantName,
                                       @RequestParam(name = "details", required = false) String details,
                                       @RequestParam(name = "zhDetails", required = false) String zhDetails,
                                       @RequestParam(name = "sortNumber", required = false) Integer sortNumber,
                                       @RequestParam(name = "status", required = false) String status,
                                       @RequestParam(name = "connectPhone", required = false) String connectPhone,
                                       @RequestParam(name = "address", required = false) String address,
                                       @RequestParam(name = "zhAddress", required = false) String zhAddress) {
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        ProductRestaurant productrestaurant = new ProductRestaurant();
        productrestaurant.setId(id);
        productrestaurant.setLogo(logo);
        productrestaurant.setCover(cover);
        productrestaurant.setPushPhoto(pushPhoto);
        productrestaurant.setRestaurantName(restaurantName);
        productrestaurant.setZhRestaurantName(zh_restaurantName);
        productrestaurant.setDetails(details);
        productrestaurant.setZhDetails(zhDetails);
        productrestaurant.setSortNumber(sortNumber);
        productrestaurant.setStatus(status);
        productrestaurant.setConnectPhone(connectPhone);
        productrestaurant.setAddress(address);
        productrestaurant.setZhAddress(zhAddress);

        page.setRecords(queryProductStoreService.findProductRestaurantPage(page, productrestaurant));

        return SuccessTip.create(page);
    }*/
}
